﻿/**********************************************
 * 功能名称：中间产品相关参数信息录入页面
编写人：  杨莹
建立时间： 杨 2008年11月8日
说明：
加载页面时，根据当前用户ID查询出责任部门，列表显示其作业中心列表。
作业中心列表中选择后，列表出作业中心中间体；
选择作业中心中间体，录入当前中间体的转出路径。
 **************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.UserControl;
using Hisun.Controllers;
using System.Text;
using System.Collections.Generic;




namespace Hisun.View.PIPDeliverInfo
{
    public partial class PIPDiliverInfo : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();
                DropDownList_CostCenterId.Items.Clear();
                var myquery = from a in db.TABC_JS_CostCenter select a;
                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID), myquery);
            }
        }

        //查询当前操作员对应的作业中心列表
        private void FillData2DropDownList(int CenterID, IEnumerable<TABC_JS_CostCenter> TotalTrees)
        {
            var Mylist = (from m in TotalTrees where m.上级作业中心ID == CenterID select m).ToList();

            if (Mylist.Count() == 0)
                return;
            foreach (var MylistItem in Mylist)
            {
                ListItem InsertItem = new ListItem(MylistItem.作业中心名称, MylistItem.作业中心ID.ToString());
                FillData2DropDownList(MylistItem.作业中心ID, TotalTrees);

              if( new string[] {"BF","BG","BH","BZ"}.Contains(MylistItem.类型) && MylistItem.停用 == false)
              {
                  DropDownList_CostCenterId.Items.Add(InsertItem);
              }
                //当作业中心类型为部门的时候，就不加入这个项目
                //var IsOrganCenter = (from b in db.TABC_JS_CostCenter where b.作业中心ID == MylistItem.作业中心ID && b.类型 == "O" select b).Count();
                //if (IsOrganCenter == 0)
                //{
                //    dropCostCenterSelect.Items.Add(InsertItem);
                //}
              

            }
        }


        private void BindMyData()
        {


            int myCenterid = Convert.ToInt32(DropDownList_CostCenterId.SelectedValue);
            string PIPid = DropDownList_PIPId.SelectedValue;


            var myquery = from a in db.TABC_JS_ProductInProcessDeliveryDetail
                          join b in db.TABC_JS_CostCenter on a.目标作业中心ID equals b.作业中心ID
                          join c in db.TABC_JS_ProductInProcess on a.目标中间体ID equals c.中间体ID
                          where a.作业中心ID == myCenterid
                              && a.会计期间ID == Application["accountPeriod"].ToString() && a.中间体ID == PIPid
                          select new                         
                          { 
                          a.目标中间体ID,
                          a.目标作业中心ID,
                          目标作业中心名称 = b.作业中心名称,
                          目标中间体名称 =c.中间体名称,
                          a.转出数量,
                          a.计量单位,
                          a.ID                       
                          };

            GridView_PIPDetail.DataSource = myquery;
            GridView_PIPDetail.DataBind();
        
        
        
        
        }

        protected void Button_Query_Command(object sender, CommandEventArgs e)
        {
            BindMyData();
        }




        //
        protected void Button_Del_Command(object sender, CommandEventArgs e)
        {
            string myid = e.CommandArgument.ToString();
            var mydelquery = (from a in db.TABC_JS_ProductInProcessDeliveryDetail where a.ID == Convert.ToInt32(myid) select a).Single();
            db.TABC_JS_ProductInProcessDeliveryDetail.DeleteOnSubmit(mydelquery);
            try
            {

                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('删除成功'); </script>");
                BindMyData(); 
            }
            catch {

                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('删除失败'); </script>");
            
            }
           

        }



        //编辑的按钮。
        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            var MyEditQuery = (from a in db.TABC_JS_ProductInProcessDeliveryDetail
                              join b in db.TABC_JS_CostCenter
                                   on a.目标作业中心ID equals b.作业中心ID
                              join c in db.TABC_JS_ProductInProcess
                              on a.目标中间体ID equals c.中间体ID 
                              where  a.ID == int.Parse(e.CommandArgument.ToString())
                              select new { 
                                  a.ID,
                              a.目标作业中心ID ,
                              b.作业中心名称,
                              a.目标中间体ID,
                              c.中间体名称,
                              c.计量单位,
                              a.转出数量                                                                                         
                            }).Single();

            EditPIPId.Value = MyEditQuery.ID.ToString();//保存当前中间体ID，以保证这个保存的时候按ID保存。
          
            TextBox_GoalPIPId.Text = MyEditQuery.目标中间体ID;
            TextBox_GoalPIPName.Text = MyEditQuery.中间体名称;

            TextBox_GoalCenterId.Text = MyEditQuery.目标作业中心ID.ToString();
            TextBox_GoalCenterName.Text = MyEditQuery.作业中心名称;
  
            TextBox_Unit.Text = MyEditQuery.计量单位;
            TextBox_Amount.Text = MyEditQuery.转出数量.ToString();




            
            TextBox_GoalCenterName.Enabled = false;
            TextBox_GoalPIPId.Enabled = false;
             TextBox_GoalPIPName.Enabled = false;
            TextBox_Unit.Enabled = false;

            EditFlag.Value = "edit";
            Panel_CenterIdAndPIPid.Enabled = false;
            Panel_EditPanel.Visible = true;
            Panel_GridView.Visible = false;

        }




        //增加的按钮事件
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {

            TextBox_GoalCenterName.Text = "";
          //  TextBox_GoalPIPName.Text = "";
            TextBox_Unit.Text = "";
            TextBox_Amount.Text = "";
            TextBox_GoalPIPId.Text = "";
            TextBox_GoalPIPName.Text = "";

            EditFlag.Value = "add";

            Panel_EditPanel.Visible = true;
            Panel_GridView.Visible = false;
            Panel_CenterIdAndPIPid.Enabled = false;


        }
        //保存的按钮事件
        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {

          

            if (EditFlag.Value == "add")                      
            {

                string[] MyGoalCenterIDandPIPID = CenterAndPIPInfo.Value.Split('&');

                if (MyGoalCenterIDandPIPID[0] == DropDownList_CostCenterId.SelectedValue)
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('目标作业中心不能等于当前作业中心'); </script>");
                    return;
                }

                var IsExistRow = (from m in db.TABC_JS_ProductInProcessDeliveryDetail
                                  where m.会计期间ID == Application["accountPeriod"].ToString()
                                   && m.作业中心ID == Convert.ToInt32(DropDownList_CostCenterId.SelectedValue)
                                   && m.中间体ID == DropDownList_PIPId.SelectedValue
                                   && m.目标作业中心ID == Convert.ToInt32(MyGoalCenterIDandPIPID[0])
                                   && m.目标中间体ID == MyGoalCenterIDandPIPID[2]
                                  select m).Count();



                if (IsExistRow != 0)
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('当前路径已存在'); </script>");
                    return;                                                
                }


                var IsInvalidGoalPIP = from n in db.TABC_JS_ProductInProcess
                                        where n.作业中心ID == Convert.ToInt32(MyGoalCenterIDandPIPID[0])
                                        && n.中间体ID == MyGoalCenterIDandPIPID[2]
                                        select n;

                if (IsInvalidGoalPIP.Count() != 1)
                {
                   // Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('目标作业中心及目标中间体不存在'); </script>");
                    Response.Write(" <script>alert('目标作业中心及目标中间体不存在'); </script>");
                    return;
                }




                TABC_JS_ProductInProcessDeliveryDetail MyNewRow = new TABC_JS_ProductInProcessDeliveryDetail();
                MyNewRow.会计期间ID = Application["accountPeriod"].ToString();
                MyNewRow.时间 = DateTime.Now;
                MyNewRow.操作员ID = Session[ServerVars.sysSessionUserName].ToString();
                MyNewRow.计量单位 = TextBox_Unit.Text;
                MyNewRow.目标中间体ID = TextBox_GoalPIPId.Text ; ;
                MyNewRow.目标作业中心ID = Convert.ToInt32(TextBox_GoalCenterId.Text);
                MyNewRow.计量单位 = MyGoalCenterIDandPIPID[4];
                MyNewRow.作业中心ID = Convert.ToInt32(DropDownList_CostCenterId.SelectedValue);
                MyNewRow.中间体ID = DropDownList_PIPId.SelectedValue;
                MyNewRow.转出数量 = Convert.ToDecimal(TextBox_Amount.Text);


                db.TABC_JS_ProductInProcessDeliveryDetail.InsertOnSubmit(MyNewRow);


                try
                {

                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加成功'); </script>");
                    
                    
                    Panel_EditPanel.Visible = false;
                    Panel_GridView.Visible = true;
                    EditFlag.Value = "";
                    CenterAndPIPInfo.Value = "";
                    Panel_CenterIdAndPIPid.Enabled = true;


                    BindMyData(); 
                }
                catch
                {

                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加失败'); </script>");
                }
                
            
            }
            else if (EditFlag.Value == "edit")
            {
                //编辑只能修改数量。
                var myquery = (from a in db.TABC_JS_ProductInProcessDeliveryDetail where a.ID == int.Parse(EditPIPId.Value) select a).Single();

                myquery.转出数量 = Convert.ToDecimal(TextBox_Amount.Text);

                try
                {

                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('保存成功'); </script>");
                    BindMyData(); 
                    
                    Panel_EditPanel.Visible = false;
                    Panel_GridView.Visible = true;
                    EditFlag.Value = "";
                    Panel_CenterIdAndPIPid.Enabled = true;

             
                    TextBox_GoalCenterName.Enabled = true;
                    TextBox_GoalPIPId.Enabled = true;
                    TextBox_GoalPIPName.Enabled = true;
                    TextBox_Unit.Enabled = true;
                }
                catch 
                
                {

                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('保存失败'); </script>");
                }
                

            
            
            }
        }

        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            
            TextBox_GoalCenterName.Text = "";
            //  TextBox_GoalPIPName.Text = "";
            TextBox_Unit.Text = "";
            TextBox_Amount.Text = "";
            TextBox_GoalPIPId.Text = "";
            TextBox_GoalPIPName.Text = "";

            if (EditFlag.Value == "edit")
            {

              
                TextBox_GoalCenterName.Enabled = true;
                TextBox_GoalPIPId.Enabled = true;
                TextBox_GoalPIPName.Enabled = true;
                TextBox_Unit.Enabled = true;
            
            
            
            }
            Panel_CenterIdAndPIPid.Enabled = true;
            EditFlag.Value = "";
            Panel_EditPanel.Visible = false;
            Panel_GridView.Visible = true;

        }


        //选择作业中心ID，弹出作业中心ID的选择框
        protected void Button_Center_Click(object sender, EventArgs e)
        {
            StringBuilder s = new StringBuilder();
            s.Append("<script language=javascript>");
            s.Append("var a=window.showModalDialog('PIPDiliverInfoSelectCenter.aspx?centerid="+DropDownList_CostCenterId.SelectedValue+"&pipid="+DropDownList_PIPId.SelectedValue+"');");
            s.Append("if(a!=null)");
            s.Append(@"{ var aa =a.split(""&"") ;");
            s.Append("document.all('TextBox_GoalCenterId').value=aa[0];");
            s.Append("document.all('TextBox_GoalCenterName').value=aa[1];");
            s.Append("document.all('TextBox_GoalPIPId').value=aa[2];");
            s.Append("document.all('TextBox_Unit').value=aa[4];");
            s.Append("document.all('TextBox_GoalPIPName').value=aa[3];");
            s.Append("document.all('CenterAndPIPInfo').value=a;}");
            s.Append("</script>");
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            string sname = "lt";
            if (!cs.IsStartupScriptRegistered(cstype, sname))
            { cs.RegisterStartupScript(cstype, sname, s.ToString()); }

        }







        
            
    }
}

